package com.gt.stream

import com.gt.SCUtil
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.streaming.{Durations, Seconds, StreamingContext}

import java.time.Duration

object Streaming_WC_01 {

  def main(args: Array[String]): Unit = {

    /**
     * 启动 netcat
     * nc -lk 9999
     */


    //1. 创建上下文对象
    val conf: SparkConf = new SparkConf().setAppName("xx").setMaster("local[*]")
    val ssc = new StreamingContext(conf, Durations.seconds(5))

    //2. 读取数据
    val hostName = "localhost"
    val port = 9999
    val srcStream: ReceiverInputDStream[String] = ssc.socketTextStream(hostName, port)

    //3. 业务逻辑处理
    val resultStream: DStream[(String, Int)] = srcStream.flatMap(_.split(" "))
      .map((_, 1))
      .reduceByKey((a, b) => a + b)

    resultStream.print()


    ssc.start()
    ssc.awaitTermination()
  }

}
